package component.show.control
{
	import common.utils.ScaleBitmap;
	
	import flash.display.*;
	import flash.geom.*;
	
	public class UISkin extends Bitmap
	{
		protected var _originalBitmap:BitmapData;
		protected var _scale9Grid:Rectangle = null;
		private var scaleBitmap:ScaleBitmap;
		
		public function UISkin(bitData:BitmapData, rec:Rectangle = null)
		{
			this._scale9Grid = rec;
			this._originalBitmap = bitData;
			super(this._originalBitmap);
			return;
		}
		
		public function clone() : UISkin
		{
			var _loc_1:* = new UISkin(this.bitmapData, this.scale9Grid);
			return _loc_1;
		}
		
		public function dispose() : void
		{
			if (this._originalBitmap != super.bitmapData)
			{
				super.bitmapData.dispose();
			}
			super.bitmapData = null;
			this._originalBitmap = null;
			return;
		}
		
		override public function set bitmapData(bitData:BitmapData) : void
		{
			this._originalBitmap = bitData;
			if (this._scale9Grid != null)
			{
				if (!this.validGrid(this._scale9Grid))
				{
					this._scale9Grid = null;
				}
				this.setSize(bitData.width, bitData.height);
			}
			else
			{
				this.assignBitmapData(this._originalBitmap);
			}
			return;
		}
		
		override public function set width(w:Number) : void
		{
			if (w != width)
			{
				this.setSize(w, height);
			}
			return;
		}
		
		override public function set height(h:Number) : void
		{
			if (h != height)
			{
				this.setSize(width, h);
			}
			return;
		}
		
		override public function set scale9Grid(rec:Rectangle) : void
		{
			var _loc_2:Number = NaN;
			var _loc_3:Number = NaN;
			if (this._scale9Grid == null && rec != null || this._scale9Grid != null && !this._scale9Grid.equals(rec))
			{
				if (rec == null)
				{
					_loc_2 = width;
					_loc_3 = height;
					this._scale9Grid = null;
					this.assignBitmapData(this._originalBitmap);
					this.setSize(_loc_2, _loc_3);
				}
				else
				{
					if (!this.validGrid(rec))
					{
						throw new Error("#001 - The _scale9Grid does not match the original BitmapData");
					}
					this._scale9Grid = rec.clone();
					this.resizeBitmap(width, height);
					scaleX = 1;
					scaleY = 1;
				}
			}
			return;
		}
		
		private function assignBitmapData(bitData:BitmapData) : void
		{
			if (super.bitmapData && super.bitmapData != this._originalBitmap)
			{
				super.bitmapData.dispose();
			}
			super.bitmapData = bitData;
			return;
		}
		
		private function validGrid(rec:Rectangle) : Boolean
		{
			return rec.right <= this._originalBitmap.width && rec.bottom <= this._originalBitmap.height;
		}
		
		override public function get scale9Grid() : Rectangle
		{
			return this._scale9Grid;
		}
		
		public function setSize(w:Number, h:Number) : void
		{
			var _loc_3:* = undefined;
			var _loc_4:* = undefined;
			if (this._scale9Grid == null)
			{
				super.width = w;
				super.height = h;
			}
			else
			{
				_loc_3 = Math.max(w, this._originalBitmap.width - this._scale9Grid.width);
				_loc_4 = Math.max(h, this._originalBitmap.height - this._scale9Grid.height);
				this.resizeBitmap(_loc_3, _loc_4);
			}
			return;
		}
		
		public function getOriginalBitmapData() : BitmapData
		{
			return this._originalBitmap;
		}
		
		protected function resizeBitmap(w:Number, h:Number) : void
		{
			var _loc_8:Rectangle = null;
			var _loc_9:Rectangle = null;
			var _loc_12:int = 0;
			var _loc_3:* = new BitmapData(w, h, true, 0);
			var _loc_4:Array = [0, this._scale9Grid.top, this._scale9Grid.bottom, this._originalBitmap.height];
			var _loc_5:Array = [0, this._scale9Grid.left, this._scale9Grid.right, this._originalBitmap.width];
			var _loc_6:Array = [0, this._scale9Grid.top, h - (this._originalBitmap.height - this._scale9Grid.bottom), h];
			var _loc_7:Array = [0, this._scale9Grid.left, w - (this._originalBitmap.width - this._scale9Grid.right), w];
			var _loc_10:* = new Matrix();
			var _loc_11:int = 0;
			while (_loc_11 < 3)
			{
				
				_loc_12 = 0;
				while (_loc_12 < 3)
				{
					
					_loc_8 = new Rectangle(_loc_5[_loc_11], _loc_4[_loc_12], _loc_5[(_loc_11 + 1)] - _loc_5[_loc_11], _loc_4[(_loc_12 + 1)] - _loc_4[_loc_12]);
					_loc_9 = new Rectangle(_loc_7[_loc_11], _loc_6[_loc_12], _loc_7[(_loc_11 + 1)] - _loc_7[_loc_11], _loc_6[(_loc_12 + 1)] - _loc_6[_loc_12]);
					_loc_10.identity();
					_loc_10.a = _loc_9.width / _loc_8.width;
					_loc_10.d = _loc_9.height / _loc_8.height;
					_loc_10.tx = _loc_9.x - _loc_8.x * _loc_10.a;
					_loc_10.ty = _loc_9.y - _loc_8.y * _loc_10.d;
					_loc_3.draw(this._originalBitmap, _loc_10, null, null, _loc_9, true);
					_loc_12++;
				}
				_loc_11++;
			}
			this.assignBitmapData(_loc_3);
			return;
		}
		
	}
}
